//index.js
//获取应用实例
const app = getApp()
const updateManager = wx.getUpdateManager();
const mta = require('../../mta_sdk/mta_analysis.js');
Page({
  data: {
    userAvatar:null,
    peopleCat:[],
    thingCat:[],
    thingQuestion:[],
    peopleQuestion: [],
    peopleIndex:0,
    thingIndex:0,
    ishome:true,
    currThingName:'',
    config:''
  },
  onShow(){
    if(this.data.userAvatar!=''){
      this.setData({
        userAvatar:wx.getStorageSync('userInfo').avatarUrl
      });
    }
  },
  onLoad: function (opt) {

    if(opt.appid){
      app.openApp(opt.appid,decodeURIComponent(opt.page) || '/pages/index/index');
    }
    this.getPeopleCategory();
    this.getThingCategory();
    updateManager.onCheckForUpdate(function (res) {})
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            updateManager.applyUpdate()
          }
        }
      })
    })
    mta.Page.init();
    this.getConfig();
  },
  //获取测试人格分类
  getPeopleCategory(){
    app.ajax({
      url: "Game/get_people_category",
    }).then(res => {
      this.setData({
        peopleCat:res.data
      });
      
    });
  },
  //获取测试事情分类
  getThingCategory(){
    app.ajax({
      url: "Game/get_thing_category",
    }).then(res => {
      this.setData({
        thingCat:res.data,
        currThingName: res.data[0].name
      });
      this.getThingQuestion(res.data[0].name);
    });
  },
  
  //获取事情具体列表
  getThingQuestion(name){
    app.ajax({
      url: "Game/get_thing_question",
      loading:true,
      data:{
        category:name
      }
    }).then(res => {
      if(res.code==200){
        res.data.forEach((ele,index) => {
          ele.rndNum=(Math.random()*10).toFixed(1);
          if(index==res.data.length-1){
            this.setData({
              thingQuestion:res.data
            })
          }
        });
      }
      
    });
  },
  
  //选中测试事情
  chooseThingItem(e){
    let dataset=e.currentTarget.dataset;
    let [name,index]=[dataset.name,dataset.index];
    this.setData({
      thingIndex:index,
      currThingName:name
    });
    this.getThingQuestion(name);
  },
  //获取配置
  getConfig(){
    app.ajax({
      url: "config/conf",
    }).then(res => {
      if(res.code==200){
        wx.setStorageSync('config',res.data);
        this.setData({
          config:res.data
        })
      }
      
    });
  },
  onShareAppMessage(){
    return{
      title:'塔罗心理测试屋'
    }
  }
})
